package net.uiiang.android.alkaid.utils;

import java.util.ArrayList;

import net.uiiang.android.alkaid.object.FileItem;
import net.uiiang.android.alkaid.object.FileManagerDbObject;

import org.apache.commons.io.FilenameUtils;

import android.content.Context;
import android.database.Cursor;
import android.graphics.drawable.Drawable;

public class FavoriteUtil {

    public static ArrayList<FileItem> convertCursorToFileItem(Cursor c,
            Context context) {
        ArrayList<FileItem> mItems = new ArrayList<FileItem>();
        int filePathIndex = c
                .getColumnIndexOrThrow(FileManagerDbObject.Favorite.FILE_PATH);
        int aliasIndex = c
                .getColumnIndexOrThrow(FileManagerDbObject.Favorite.ALIAS);
        int fileTypeIndex = c
                .getColumnIndexOrThrow(FileManagerDbObject.Favorite.FILE_TYPE);
        for (c.moveToFirst(); !(c.isAfterLast()); c.moveToNext()) {
            // String ccountry = c.getString(countryIndex);
            // int ccode = c.getInt(codeIndex);
            String fileName = c.getString(aliasIndex);
            String filePath = c.getString(filePathIndex);
            Drawable icon = FileUtils
                    .getFileIconWithExtensions(context, FilenameUtils
                            .getName(filePath), c.getString(fileTypeIndex));
            if (filePath != null && filePath.length() > 30) {
                filePath = Utils.shortenPath(filePath);
            }
            FileItem fileItem = new FileItem(fileName, filePath, icon, "", "",
                    false);
            mItems.add(fileItem);
        }
        return mItems;
    }

    public static ArrayList<String> convertCursorToPathList(Cursor c) {
        ArrayList<String> pathList = new ArrayList<String>();
        int filePathIndex = c
                .getColumnIndexOrThrow(FileManagerDbObject.Favorite.FILE_PATH);
        for (c.moveToFirst(); !(c.isAfterLast()); c.moveToNext()) {
            String filePath = c.getString(filePathIndex);
            pathList.add(filePath);
        }
        return pathList;
    }
}
